package com.adprof.demo.natives.listview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.NonNull;

import com.adprof.demo.android.R;
import com.adprof.demo.utils.ConstantsDemo;
import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.NativeAdData;
import com.adprof.sdk.api.NativeAdEventListener;
import com.adprof.demo.natives.viewholders.AdViewHolder;
import com.adprof.demo.natives.viewholders.CardViewHolder;
import com.adprof.demo.natives.viewholders.ImageViewHolder;
import com.adprof.demo.natives.viewholders.NewsViewHolder;
import com.adprof.demo.natives.viewholders.NormalViewHolder;
import com.adprof.demo.natives.viewholders.ProductViewHolder;
import com.adprof.demo.natives.viewholders.VideoViewHolder;
import com.adprof.demo.natives.viewholders.ViewHolderDataBinder;
import com.adprof.demo.utils.UIUtil;

import java.util.List;

/**
 * ListView适配器，用于展示原生广告和普通内容
 */
public class NativeAdListAdapter extends BaseAdapter {

    private static final int ITEM_VIEW_TYPE_NORMAL = 0;
    private static final int ITEM_VIEW_TYPE_AD = 1;
    private static final int ITEM_VIEW_TYPE_CARD = 2;
    private static final int ITEM_VIEW_TYPE_IMAGE = 3;
    private static final int ITEM_VIEW_TYPE_VIDEO = 4;
    private static final int ITEM_VIEW_TYPE_NEWS = 5;
    private static final int ITEM_VIEW_TYPE_PRODUCT = 6;
    private final List<NativeAdData> mData;
    private final Activity mActivity;

    public NativeAdListAdapter(Activity activity, List<NativeAdData> data) {
        this.mActivity = activity;
        this.mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public NativeAdData getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //信息流广告的样式, 通过 ad.getAdPatternType 接口判断
    @Override
    public int getItemViewType(int position) {
        NativeAdData ad = getItem(position);
        if (ad == null) {
            // 根据位置返回不同的普通内容类型
            int normalType = position % 6;
            switch (normalType) {
                case 0:
                    return ITEM_VIEW_TYPE_CARD;
                case 1:
                    return ITEM_VIEW_TYPE_IMAGE;
                case 2:
                    return ITEM_VIEW_TYPE_VIDEO;
                case 3:
                    return ITEM_VIEW_TYPE_NEWS;
                case 4:
                    return ITEM_VIEW_TYPE_PRODUCT;
                default:
                    return ITEM_VIEW_TYPE_NORMAL;
            }
        } else {
            return ITEM_VIEW_TYPE_AD;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 7;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NativeAdData ad = getItem(position);
        switch (getItemViewType(position)) {
            case ITEM_VIEW_TYPE_AD:
                return getADItemView(convertView, parent, ad);
            case ITEM_VIEW_TYPE_CARD:
                return getCardItemView(convertView, parent, position);
            case ITEM_VIEW_TYPE_IMAGE:
                return getImageItemView(convertView, parent, position);
            case ITEM_VIEW_TYPE_VIDEO:
                return getVideoItemView(convertView, parent, position);
            case ITEM_VIEW_TYPE_NEWS:
                return getNewsItemView(convertView, parent, position);
            case ITEM_VIEW_TYPE_PRODUCT:
                return getProductItemView(convertView, parent, position);
            default:
                return getNormalItemView(convertView, parent, position);
        }
    }

    //渲染视频广告，以视频广告为例，以下说明
    private View getADItemView(View convertView, ViewGroup viewGroup, @NonNull final NativeAdData nativeAdData) {
        final NativeAdViewHolder adViewHolder;
        try {
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_ad_native, viewGroup, false);
                adViewHolder = new NativeAdViewHolder(convertView);
                convertView.setTag(adViewHolder);
            } else {
                adViewHolder = (NativeAdViewHolder) convertView.getTag();
            }

            // 这2个一定要设置  且注意顺序
            nativeAdData.setNativeAdMediaListener(null);
            nativeAdData.setNativeAdEventListener(null);

            View view;

            if (nativeAdData.isExpressAd()) {
                view = nativeAdData.getFeedView();
            } else {
                view = createNativeAdView(nativeAdData, adViewHolder);
            }

            // 添加进容器
            adViewHolder.adContainer.removeAllViews();
            if (view != null) {
                UIUtil.removeViewFromParent(view);
                adViewHolder.adContainer.addView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    /**
     * 非广告list item
     *
     * @param convertView
     * @param parent
     * @param position
     * @return
     */
    @SuppressWarnings("RedundantCast")
    @SuppressLint("SetTextI18n")
    private View getNormalItemView(View convertView, ViewGroup parent, int position) {
        NormalViewHolder normalViewHolder;
        if (convertView != null) {
            normalViewHolder = (NormalViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_normal, parent, false);
            normalViewHolder = new NormalViewHolder(convertView);
            convertView.setTag(normalViewHolder);
            convertView.setBackgroundColor(UIUtil.getARandomColor());
        }
        normalViewHolder.text.setText("ListView item " + position);
        return convertView;
    }

    private View createNativeAdView(NativeAdData nativeAdData, NativeAdViewHolder adViewHolder) {
        // 广告交互监听
        View adView = adViewHolder.adRender.renderAdView(nativeAdData, new NativeAdEventListener() {
            @Override
            public void onAdExposed() {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdExposed----------");
            }

            @Override
            public void onAdClicked() {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdClicked----------");
            }

            @Override
            public void onAdRenderFail(AdError error) {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdRenderFail----------" + error.toString());
            }
        });
        return adView;
    }

    /**
     * 广告ViewHolder - 使用公共的AdViewHolder
     */
    public static class NativeAdViewHolder extends AdViewHolder {
        public NativeAdViewHolder(View itemView) {
            super(itemView);
        }
    }

    // 各种item的获取方法
    private View getCardItemView(View convertView, ViewGroup parent, int position) {
        CardViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_list_card, parent, false);
            holder = new CardViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (CardViewHolder) convertView.getTag();
        }
        ViewHolderDataBinder.bindCardContent(holder, position);
        return convertView;
    }

    private View getImageItemView(View convertView, ViewGroup parent, int position) {
        ImageViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_list_image, parent, false);
            holder = new ImageViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ImageViewHolder) convertView.getTag();
        }
        ViewHolderDataBinder.bindImageContent(holder, position);
        return convertView;
    }

    private View getVideoItemView(View convertView, ViewGroup parent, int position) {
        VideoViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_list_video, parent, false);
            holder = new VideoViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (VideoViewHolder) convertView.getTag();
        }
        ViewHolderDataBinder.bindVideoContent(holder, position);
        return convertView;
    }

    private View getNewsItemView(View convertView, ViewGroup parent, int position) {
        NewsViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_list_news, parent, false);
            holder = new NewsViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (NewsViewHolder) convertView.getTag();
        }
        ViewHolderDataBinder.bindNewsContent(holder, position);
        return convertView;
    }

    private View getProductItemView(View convertView, ViewGroup parent, int position) {
        ProductViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_list_product, parent, false);
            holder = new ProductViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ProductViewHolder) convertView.getTag();
        }
        ViewHolderDataBinder.bindProductContent(holder, position);
        return convertView;
    }

}
